﻿using System;
using System.Linq;
using System.Threading.Tasks;
using HengTong.DataAccess;
using HengTong.Model.Db;
using HengTong.Model.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Serilog;
using Serilog.Core;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Support.FileHandlers;

namespace HengTong.Test;

[TestClass]
public class TestSms
{
    private ServiceProvider _serviceProvider;

    [TestInitialize]
    public void TestInitialize()
    {
        var config = InitConfiguration();
        var wtm = MockWtmContext.CreateWtmContext(
            new DataContext(
                "Server=42.225.116.55,19926;Database=HengTong;user id=sa;password=Jia123wei!;TrustServerCertificate=True",
                DBTypeEnum.SqlServer), "admin");
        wtm.SetServiceProvider(_serviceProvider);
        // 配置 Serilog
        Log.Logger = new LoggerConfiguration()
            .WriteTo.Console()
            .CreateLogger();
        var services = new ServiceCollection();
        services.AddLogging(builder => { builder.AddSerilog(dispose: true); });
        services.AddMemoryCache();
        services.AddSingleton(config);
        services.AddScoped<WtmFileProvider>();
        services.Configure<Configs>(config);
        services.AddSingleton(wtm);
        config.Get<Configs>();
        services.AddScoped<Logger>();
        services.AddScoped<SmsAuthService>();
        services.AddScoped<ISmsAuthService, SmsAuthService>();
        // services.AddSingleton<AwsTaskService>();
        // services.AddTransient<AwsTaskService>();
        _serviceProvider = services.BuildServiceProvider();
    }

    private IConfiguration InitConfiguration()
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.development.json")
            .Build();
        return config;
    }
    

        
    // 1. 发送验证码短信
    [TestMethod]
    public async Task TestSendSms()
    {
        var scope = _serviceProvider.CreateScope();
        var service = scope.ServiceProvider.GetRequiredService<ISmsAuthService>();
    
        // 测试发送短信
        var mobile = "16692218221";
        var sendResult = await service.SendCodeAsync(mobile);
        Assert.AreEqual(200, sendResult.Code);
        
    }
    
    // 2. 验证短信验证码
    [TestMethod]
    public void TestVerifySms()
    {
        var scope = _serviceProvider.CreateScope();
        var service = scope.ServiceProvider.GetRequiredService<ISmsAuthService>();
    
        // 测试验证短信验证码
        var mobile = "16692218221";
        var code = "853680"; // 假设这是之前发送的验证码
        var verifyResult = service.VerifyCode(mobile, code);
        Assert.IsTrue(verifyResult.Data);
    }
}